import logging

from django.db.models import Q
from django.utils.decorators import method_decorator
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import viewsets
from basic_info.models import SettlementAccountModel

from ERP_5.apps.basic_info.serializer.settlement_account_serializer import AccountSerializer
from ERP_5.utils.paginations import GlobalPagination
from ERP_5.utils.base_views import MultipleDestroyMixin, MultipleOpenMixin

logger = logging.getLogger('my')

param1 = openapi.Parameter(name='name', type=openapi.TYPE_STRING, description="名称的关键字", in_=openapi.IN_QUERY)
param2 = openapi.Parameter(name='remark', type=openapi.TYPE_STRING, description="备注的关键字", in_=openapi.IN_QUERY)
param3 = openapi.Parameter(name='number_code', type=openapi.TYPE_STRING, description="编号关键字", in_=openapi.IN_QUERY)


@method_decorator(name='list', decorator=swagger_auto_schema(
    manual_parameters=[param1, param2, param3],
    operation_description="账户的搜索过滤")
                  )
class AccountView(viewsets.ModelViewSet, MultipleDestroyMixin, MultipleOpenMixin):
    """
    create:
    结算账户信息--新增
    
    结算账户信息新增, status: 201(成功), return: 新增结算账户信息信息
    
    destroy:
    结算账户信息--删除
    
    结算账户信息删除, status: 204(成功), return: None
    
    multiple_delete:
    结算账户信息--批量删除,必传json参数：ids=[1,2,3,4...]
    
    结算账户信息批量删除, status: 204(成功), return: None

    multiple_open:
    结算账户信息--批量启用或者禁用,必传(json)参数：ids=[1,2,3,4...](列表中可以只有一个)，is_open=1/0

    {
        "ids":[1,2],
        "is_open":"0"
    }
    is_open=1表示禁用，is_open=0表示启用，
    结算账户信息批量启用或者禁用, status: 204(成功), return: None

    update:
    结算账户信息--修改,一般用于修改多个属性
    
    结算账户信息修改, status: 200(成功), return: 修改后的结算账户信息信息
    
    partial_update:
    结算账户信息--局部修改(一般修改一个属性),可以传参任意属性的值，服务器会修改指定的属性值
    
    结算账户信息局部修改, status: 200(成功), return: 修改后的结算账户信息信息
    
    list:
    结算账户信息--获取分页列表，可选json参数:name(名称)，remark(描述关键字),number_code(编号)

    {
        "name":"长沙",
        "remark":"xxx", "number_code":"xxxx"
    }
    结算账户信息列表信息, status: 200(成功), return: 结算账户信息信息列表
    
    retrieve:
    查询某一个结算账户信息
    
    查询指定ID的结算账户信息, status: 200(成功), return: 用户结算账户信息
    """

    queryset = SettlementAccountModel.objects.all()
    serializer_class = AccountSerializer
    pagination_class = GlobalPagination

    def get_queryset(self):
        if self.action == 'list':  # 过滤查询
            # 获取请求参数(在json中)：name，phone, mobile
            name = self.request.data.get('name', None)
            remark = self.request.data.get('remark', None)
            number_code = self.request.data.get('number_code', None)
            query = Q()
            if name:
                query.add(Q(name__contains=name), 'AND')  # 多条件组合
            if remark:
                query.add(Q(remark__contains=remark), 'AND')
            if remark:
                query.add(Q(number_code__contains=number_code), 'AND')
            return SettlementAccountModel.objects.filter(query).all()
        else:
            return SettlementAccountModel.objects.all()
